package com.example.controller;

import com.example.pojo.News;
import com.example.service.NewsService;
import com.example.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    //添加获取说所有新闻列表接口
    @GetMapping("/all")
    public List<News> getNewsList() {
        try {
            List<News> newsList = newsService.getNewsList();
            return newsList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //添加根据新闻分类ID获取新闻接口
    @GetMapping("/category/{categoryId}")
    public List<News> findByCategoryId(@PathVariable Integer categoryId) {
        return newsService.findByCategoryId(categoryId);
    }
    //添加新闻发布接口
    @PostMapping("/add")
    public void addNews(@RequestParam("url") MultipartFile url,
                        @RequestParam("title") String title,
                        @RequestParam("source") String source,
                        @RequestParam("categoryName") String categoryName,
                        @RequestParam("content") String content) {
        News news = new News();
        news.setTitle(title);
        news.setSource(source);
        news.setCategoryName(categoryName);
        news.setContent(content);
        String imagePath = null;
        try {
            imagePath = FileUploadUtils.processFileUpload(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        news.setUrl(imagePath);

        newsService.add(news);
    }

    //添加删除新闻接口
    @DeleteMapping("/delete/{newsId}")
    public void deleteNews(@PathVariable Integer newsId) {
        newsService.deleteById(newsId);
    }

    // 更新新闻
    @PutMapping("/update/{id}")
    public void updateById(@PathVariable Integer id,
                           @RequestParam(value = "url", required = false) MultipartFile url,
                           @RequestParam(value = "title", required = false) String title,
                           @RequestParam(value = "source", required = false) String source,
                           @RequestParam(value = "categoryName", required = false) String categoryName,
                           @RequestParam(value = "content", required = false) String content) {
        News news = newsService.getNewsById(id);
        if (title != null) {
            news.setTitle(title);
        }
        if (source != null) {
            news.setSource(source);
        }
        if (categoryName != null) {
            news.setCategoryName(categoryName);
        }
        if (content != null) {
            news.setContent(content);
        }
        if (url != null && !url.isEmpty()) {
            String imagePath = null;
            try {
                imagePath = FileUploadUtils.processFileUpload(url);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            news.setUrl(imagePath);
        }
        newsService.updateById(news);
    }
}